
let server = 'https://factory.yummyn.com/api/';//正式域名
//post请求 url：请求路径，请求header，params请求参数，app全局变量
function networkpost(url, params) {
  let sessionId = wx.getStorageSync("token")
  var header = { 'content-type': 'application/x-www-form-urlencoded', 'token': sessionId }
  wx.showLoading({
    title: '加载中',
  })
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: server+url,
      header: header,
      data: params,
      method: 'POST',
      success: function (res) {
        if (res.data.result_code == 200) {
          resolve(res)
          wx.hideLoading()
        } else {
          if (res.data.result_code == 401) {
            wx.showToast({
              title: "请重新登录",
              success(){
                wx.switchTab({
                  url: '../../pages/my/my',
                })
                wx.hideLoading()
              }
            })
          }
        }
      },

      fail(){
        wx.hideLoading()
        wx.showToast({
          title: "获取数据失败",
        })
      }

    })
  })
  return promise;
};


//get请求
function networkget(url, params) {
  let sessionId = wx.getStorageSync("token")

  var header1 = { 'content-type': 'application/json', 'token': sessionId }
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: server+url,
      header:header1,// 默认值
      data: params,
      method: 'GET',
      success: function (res) {
        if (res.data.result_code == 200) {
          resolve(res)
        } else {
          if (res.data.result_code == 401) {
            wx.showToast({
              title: "请退出重新登录",
            })
          }
        }
      }

    })
  });
  return promise;
}

module.exports = {
  networkget:networkget,
  networkpost:networkpost
}